package com.zksk.admin.service.orders.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.admin.domain.orders.dto.OrdersDto;
import com.zksk.admin.domain.orders.vo.OrdersRequestVo;
import com.zksk.admin.mapper.orders.OrdersDao;
import com.zksk.admin.service.orders.OrdersService;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.mbg.admin.domain.Orders;
import com.zksk.mbg.admin.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 订单表 服务类
 * </p>
 *
 * @author Zksk Acmen
 * @since 2022-12-28
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersDao ordersDao;

    @Override
    public IPage<OrdersDto> ordersList(OrdersRequestVo requestVo) {
        IPage<Orders> pageConf = new Page<>(requestVo.getPageNum(), requestVo.getPageSize());
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<Orders>()
                .eq(StringUtils.isNotEmpty(requestVo.getEnumProduct()), "o.enum_product_id", requestVo.getEnumProduct()) // 产品枚举id
                .like(StringUtils.isNotEmpty(requestVo.getEnumProductName()), "o.enum_product_name", requestVo.getEnumProductName()) // 产品枚举名称
                .eq(StringUtils.isNotEmpty(requestVo.getOrdersCode()), "o.orders_code", requestVo.getOrdersCode()) // 订单id
                .eq(requestVo.getOrderStatus() != null, "o.orders_status", requestVo.getOrderStatus()) // 订单状态
                .eq(StringUtils.isNotEmpty(requestVo.getStationId()), "o.stations_id", requestVo.getStationId()) // 所属平台id
                .like(StringUtils.isNotEmpty(requestVo.getStationName()), "o.stations_name", requestVo.getStationName()) // 所属平台
                .eq(StringUtils.isNotEmpty(requestVo.getOrgId()), "o.orgs_id", requestVo.getOrgId()) // 所属机构id
                .like(StringUtils.isNotEmpty(requestVo.getOrgName()), "o.orgs_name", requestVo.getOrgName()) // 所属机构
                .eq(StringUtils.isNotEmpty(requestVo.getCompanyId()), "o.member_id", requestVo.getCompanyId()) // 企业id
                .like(StringUtils.isNotEmpty(requestVo.getCompanyName()), "o.member_name", requestVo.getCompanyName()) // 企业名称
                .between(requestVo.getApplyTimeL() != null && requestVo.getApplyTimeR() != null, "o.apply_time", requestVo.getApplyTimeL(), requestVo.getApplyTimeR()) // 申请时间
                .apply(requestVo.getQuestionStatus()!=null, "oq.question_status=" + requestVo.getQuestionStatus())
                .last("and o.del_flag='0' and oq.del_flag = '0' order by o.create_time desc"); // 问题删除标识

        return ordersDao.ordersList(pageConf, queryWrapper);
    }
}
